package crm.system.action.basdaction;

import java.math.BigDecimal;
import java.util.List;

import org.apache.struts2.ServletActionContext;
import org.hibernate.Transaction;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Restrictions;

import com.opensymphony.xwork2.ActionSupport;

import crm.system.entity.BasDict;
import crm.system.service.basdservice.BasdService;
import crm.system.util.Page;

public class BasdAction extends ActionSupport {
	private BasdService basdService;
	private List<BasDict> basdList;
	private int totalCount;
	private int totalPage;
	private int pageNo;
	private BasDict basDict;
	private Long id;

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public BasDict getBasDict() {
		return basDict;
	}

	public void setBasDict(BasDict basDict) {
		this.basDict = basDict;
	}

	public int getPageNo() {
		return pageNo;
	}

	public void setPageNo(int pageNo) {
		this.pageNo = pageNo;
	}

	public int getTotalCount() {
		return totalCount;
	}

	public void setTotalCount(int totalCount) {
		this.totalCount = totalCount;
	}

	public int getTotalPage() {
		return totalPage;
	}

	public void setTotalPage(int totalPage) {
		this.totalPage = totalPage;
	}

	public List<BasDict> getBasdList() {
		return basdList;
	}

	public void setBasdList(List<BasDict> basdList) {
		this.basdList = basdList;
	}

	public void setBasdService(BasdService basdService) {
		this.basdService = basdService;
	}

	/**
	 * 分页查询列表
	 * 
	 * @return
	 */
	public String getPageList() {
		DetachedCriteria dc = DetachedCriteria.forClass(BasDict.class);
		totalCount = basdService.getTotalCount(dc);
		if (basDict != null) {
			if (basDict.getDictType() != null
					&& basDict.getDictType().length() > 0) {
				dc.add(Restrictions.ilike("dictType", basDict.getDictType(),
						MatchMode.ANYWHERE));
			}
			if (basDict.getDictItem() != null
					&& basDict.getDictItem().length() > 0) {
				dc.add(Restrictions.ilike("dictItem", basDict.getDictItem(),
						MatchMode.ANYWHERE));
			}
			if (basDict.getDictValue() != null
					&& basDict.getDictValue().length() > 0) {
				dc.add(Restrictions.ilike("dictValue", basDict.getDictValue(),
						MatchMode.ANYWHERE));
			}
		}
		int pageSize = 4;
		Page page = new Page();
		page.setPageSize(pageSize);
		page.setTotalCount(totalCount);
		page.setTotalPages();
		page.setPageNo(pageNo);
		pageNo = (pageNo - 1) * pageSize;
		basdList = basdService.getPageList(dc, pageNo, pageSize);
		ServletActionContext.getRequest().setAttribute("page", page);
		return SUCCESS;
	}

	/**
	 * 获取一条记录
	 */
	public String doUpdate() {
		basDict = basdService.getById(id);
		if (basDict != null) {
			return SUCCESS;
		} else {
			return INPUT;
		}
	}

	/**
	 * 修改方法
	 */
	public String update() {
		try {
			if (basDict.getDictIsEditable().equals("1")) {
				basDict.setDictIsEditable("0");
				basdService.update(basDict);
			}
			if (basDict.getDictIsEditable().equals("0")) {
				basDict.setDictIsEditable("1");
				basdService.update(basDict);
			}
			return SUCCESS;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return INPUT;
		}
	}

	/**
	 * 添加方法
	 */
	public String add() {
		try {
			if (basDict.getDictIsEditable().equals("1")) {
				basDict.setDictIsEditable("0");
				basdService.add(basDict);
				
			}
			if (basDict.getDictIsEditable().equals("0")) {
				basDict.setDictIsEditable("1");
				basdService.add(basDict);
				
			}
			return SUCCESS;
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return INPUT;
		}
	}
	
	public String delete(){
		try {
			basdService.delete(id);
			return SUCCESS;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return INPUT;
		}
		
		
	}
}
